package com.qire.common.dal.base;

import com.qire.antsbinder.dal.exception.ExceptionObservable;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.dal.exception.DisableAccountException;
import com.qire.common.dal.exception.NetApiException;
import com.qire.common.dal.exception.NotLoginException;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.utils.ToastHelper;

import java.io.Serializable;

public class ResponseWrapper implements Serializable {

    private static final long serialVersionUID = 1;

    /**
     * 错误代码：未登录
     */
    public static final int EC_NOT_LOGIN = 10005;
    /**
     * 错误代码：账户被禁用
     */
    public static final int EC_ACCOUNT_DISABLE = 11111;

    private int state;
    private int status;
    private String req;
    private String msg;
    private Object data;
    private String logid;
    private double usetime;
    private int errCode;
    private long st;    //服务器同步时间: 1619582929

    public int getState() {
        return state;
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public boolean isIllegal(){
        return getState() == 0 || getStatus() < 1;
    }

    /**
     * 同步服务器时间
     * @return
     */
    public ResponseWrapper syncServiceTime() {
        if(st > 0) {
            ServiceTimer.syncServiceTime(st * 1000);
        }
        return this;
    }

    /**
     * 该方法是当请求失败时调用，isIllegal返回true时，需要更具服务器返回的错误代码进行处理，或者是其他方式处理错误情况*/
    public void illegalExceptionHandle(String tagUrl){
        switch (errCode) {
            case EC_NOT_LOGIN :
                NotLoginException notLoginException = new NotLoginException(tagUrl);
                notLoginException.requestUrl = tagUrl;
                notLoginException.extraMsg = getMsg();
                ExceptionObservable.notifyObservers(notLoginException);
                return;
            case EC_ACCOUNT_DISABLE :
                UserEntity.self.logout();
                AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.LoginGuidePage)
                        .withData("disableMessage", msg)
                        .go();

                DisableAccountException disableAccountException = new DisableAccountException(tagUrl);
                ExceptionObservable.notifyObservers(disableAccountException);
                return;
            default :
                NetApiException netApiException = new NetApiException(tagUrl);
                netApiException.requestUrl = tagUrl;
                netApiException.errorCode = errCode;
                netApiException.extraMsg = getMsg();
                ExceptionObservable.notifyObservers(netApiException);
                return;
        }
    }

}
